#pragma once
#include "ReadOP2.h"
//#include <Python.h>
#include <iostream>
#include <cstdlib>
using namespace std;

void ReadOP2::setInputFile(std::string fileName)
{
	vector<string> temp_vec;
	auto a = fileName.find_last_of('/');
	temp_vec.emplace_back(fileName.substr(0,a));
	temp_vec.emplace_back(fileName.substr(a+1));
	op2_fileName = temp_vec.back();
	read_path = temp_vec[0];
	size_t dot_pos = temp_vec.back().find_last_of('.');

	vtu_fileName = temp_vec.back().substr(0, dot_pos) + ".vtu";
	msh_fileName = temp_vec.back().substr(0, dot_pos) + ".msh";
}

void ReadOP2::setOutputDir(std::string dirName)
{
	outDirpath = dirName;
}

void ReadOP2::setExeFilePath(std::string exePath)
{
	this->exe_path = exePath;
}
#if 0
int ReadOP2::writeVTKFile()
{
	Py_Initialize();    // 初始化
	PyRun_SimpleString("import sys;print(dir(sys.path))");
	// 将py文件所在路径添加到搜索路径中
	PyRun_SimpleString("import sys");
	PyObject* sys = PyImport_ImportModule("sys");
	PyObject* path = PyObject_GetAttrString(sys, "path");
	string py_file_path = exe_path;
	PyList_Append(path, PyUnicode_FromString(py_file_path.c_str()));

	//PyRun_SimpleString("sys.path.append('./scripts')");
	// 加载模块
	PyObject* pModule = PyImport_ImportModule("op2convert");
	if (!pModule)
	{
		PyErr_Print();
		cout << "[ERROR] Python get module failed." << endl;
		Py_Finalize();      // 释放资源
		return -1;
	}
	else {
		cout << "[INFO] Python get module succeed." << endl;
	}
	// 加载函数
	PyObject* pv = PyObject_GetAttrString(pModule, "op2vtk");
	if (!pv || !PyCallable_Check(pv))
	{
		PyErr_Print();
		cout << "[ERROR] Can't find funftion." << endl;
		Py_Finalize();      // 释放资源
		return -1;
	}
	cout << "[INFO] Get function succeed." << endl;


	// 准备参数
	// 将参数转换为 Python 对象
	PyObject* pArgs = PyTuple_New(3);
	std::string tmpop2 = outDirpath + '/'+op2_fileName;
	std::string tmpvtk = outDirpath + '/'+vtu_fileName;
	PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(tmpop2.c_str()));
	PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tmpop2.c_str()));
	PyTuple_SetItem(pArgs, 2, PyUnicode_FromString(tmpvtk.c_str()));

	// 调用函数
	PyObject* pRet = PyObject_CallObject(pv, pArgs);
	if (!pRet)
	{
		PyErr_Print();
		cout << "[ERROR] CallObject throw a exception" << endl;
		Py_Finalize();      // 释放资源
		return -1;
	}
	Py_Finalize();      // 释放资源
	return 0;
}
#endif


int ReadOP2::writeMSHFile()
{
	std::cout<<"start initialize"<<std::endl;
	Py_Initialize();
	std::cout<<"initialize finished"<<std::endl;
	PyRun_SimpleString("import sys;print(dir(sys.path))");
	PyObject* pModule = PyImport_ImportModule("ConvertOp2");
	if (!pModule)
	{
		PyErr_Print();
		cout << "[ERROR] Python get module failed." << endl;
		Py_Finalize();      // 释放资源
		return -1;
	}
	else {
		cout << "[INFO] Python get module succeed." << endl;
	}
	PyObject* pv2 = PyObject_GetAttrString(pModule, "ConvertOp2");
	PyObject* pArgs2 = PyTuple_New(2);
	std::string filepath = read_path + '/'+op2_fileName;
	string outpath = outDirpath;
	PyTuple_SetItem(pArgs2, 0, PyUnicode_FromString(filepath.c_str()));
	PyTuple_SetItem(pArgs2, 1, PyUnicode_FromString(outpath.c_str()));
	std::cout<<"start convert .op2 to .vtu..."<<std::endl;
	PyObject* pRet2 = PyObject_CallObject(pv2, pArgs2);
	if (!pRet2)
	{
		PyErr_Print();
		cout << "[ERROR] CallObject throw a exception" << endl;
		Py_Finalize();      // 释放资源
		return -1;
	}
	// PyObject* pv = PyObject_GetAttrString(pModule, "vtu_to_msh");
	// if (!pv || !PyCallable_Check(pv))
	// {
	// 	PyErr_Print();
	// 	cout << "[ERROR] Can't find funftion." << endl;
	// 	Py_Finalize();      // 释放资源
	// 	return -1;
	// }
	// PyObject* pArgs = PyTuple_New(2);
	// std::string temp_vtu = outDirpath + '/'+vtu_fileName;
	// std::string tmpmsh = outDirpath + '/'+msh_fileName;
	// PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(temp_vtu.c_str()));
	// PyTuple_SetItem(pArgs, 1, PyUnicode_FromString(tmpmsh.c_str()));
	// std::cout<<"start convert .vtu to .msh..."<<std::endl;
	// PyObject* pRet = PyObject_CallObject(pv, pArgs);
	// if (!pRet)
	// {
	// 	PyErr_Print();
	// 	cout << "[ERROR] CallObject throw a exception" << endl;
	// 	Py_Finalize();      // 释放资源
	// 	return -1;
	// }
	Py_Finalize();      // 释放资源
	return 0;
}

